package com.community.zhihuishequ.view.collect;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.community.zhihuishequ.R;
import com.community.zhihuishequ.db.HouseData;
import com.community.zhihuishequ.view.base.BaseHostActivity;
import com.shuhart.stepview.StepView;

import org.apache.commons.lang3.ArrayUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CollectFragmentActivity extends BaseHostActivity {

    @BindView(R.id.toolBar)
    Toolbar toolBar;
    @BindView(R.id.appBarLayout)
    AppBarLayout appBarLayout;
    @BindView(R.id.fragmnet)
    FrameLayout fragmnet;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.step_view)
    StepView stepView;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.btn_save)
    Button btnSave;

    private HouseData houseData;
    /**
     * type : 1 业主
     * ：2 租户
     */
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect_fragment);
        unbinder = ButterKnife.bind(this);
        initActionBar(toolBar, true);
        toolBar.setNavigationIcon(R.drawable.ic_back);
        initData();
        houseData = new HouseData();
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            type = intent.getIntExtra("type", 0);
        }
        if (type == 1) {
            transFragment(getResources().getString(R.string.collect_owner_tab1));
            tvTitle.setText(getResources().getString(R.string.collect_owner_tab1));
        } else if (type == 2) {
            transFragment(getResources().getString(R.string.collect_lessee_tab1));
            tvTitle.setText(getResources().getString(R.string.collect_lessee_tab1));
        }
        initStepView();
    }

    private void initStepView() {
        List<String> stepList = new ArrayList<>();
        if (type == 1) {
            stepList.add(getResources().getString(R.string.collect_owner_tab1));
            stepList.add(getResources().getString(R.string.collect_owner_tab2));
        } else if (type == 2) {
            stepList.add(getResources().getString(R.string.collect_lessee_tab1));
            stepList.add(getResources().getString(R.string.collect_lessee_tab2));
        }
        stepView.setSteps(stepList);
    }

    @Override
    public Fragment createFragment(String fragmentTag) {
        String[] tabs = null;
        if (type == 1) {
            tabs = getResources().getStringArray(R.array.collect_owner_tabs);
        } else if (type == 2) {
            tabs = getResources().getStringArray(R.array.collect_lessee_tabs);
        }
        switch (ArrayUtils.indexOf(tabs, fragmentTag)) {
            case 0:
                return new CollectOwnerFragment();
            case 1:
                return new CollectHouseFragment();
            default:
                return null;
        }
    }

    @Override
    public int getContainerId() {
        return R.id.fragmnet;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick(R.id.btn_submit)
    public void onBtnSubmitClicked() {
        if (curFragment() instanceof CollectOwnerFragment) {
            houseData = ((CollectOwnerFragment) curFragment()).saveData(houseData);
            if (type == 1) {
                transFragment(getResources().getString(R.string.collect_owner_tab2));
                tvTitle.setText(getResources().getString(R.string.collect_owner_tab2));
            } else if (type == 2) {
                transFragment(getResources().getString(R.string.collect_lessee_tab2));
                tvTitle.setText(getResources().getString(R.string.collect_lessee_tab2));
            }
            stepView.go(1, true);
        } else if (curFragment() instanceof CollectHouseFragment) {
            ((CollectHouseFragment) curFragment()).saveData(houseData);
            ToastUtils.showShort("保存成功！");
            finish();
        }
    }

    @OnClick(R.id.btn_save)
    public void onBtnSaveClicked() {
        finish();
        ((CollectHouseFragment) curFragment()).saveData(houseData);
        ToastUtils.showShort("保存成功！");
    }
}
